package com.softserve.service;

//JAX-RS Imports
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import com.softserve.dao.StudentDAO;
import com.softserve.entity.Student;
import com.softserve.entity.Students;

@Path("/studentservice")
@Produces({"application/json","application/xml"})
public class StudentService {

	private StudentDAO studentDAO;

	public StudentDAO getStudentDAO() {
		return studentDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	@GET
	@Path("/student")
	@Produces({"application/json","application/xml"})
	public Students getStudents() {

		System.out.println("getStudents called ");
		
		Students result = new Students();
		result.setStudent(getStudentDAO().getAllStudents());
		return result;
	}

	@GET
	@Path("/student")
	@Produces({"application/json","application/xml"})
	public Students searchStudents(@QueryParam("name") String searchName) {

		System.out.println("searchStudents called ");
		
		Students result = new Students();
		result.setStudent(getStudentDAO().getAllStudents());
		return result;
	}
	
	@GET
	@Path("/student/{id}")
	@Produces({"application/json","application/xml"})
	public Student getStudent(@PathParam("id") long id) {

		System.out.println("getStudent called with student id: " + id);

		Student student = (Student) getStudentDAO().getStudent(id);
		if (student == null) {
			ResponseBuilder builder = Response.status(Status.BAD_REQUEST);
			builder.type("application/xml");
			builder.entity("<error>Student Not Found</error>");
			throw new WebApplicationException(builder.build());
		} else {
			return student;
		}
	}

	@POST
	@Path("/student")
	@Consumes({"application/json","application/xml"})
	public Response addStudent(Student student) {

		System.out.println("addStudent called");

		Student stud = (Student) getStudentDAO().getStudent(
				student.getId());

		if (stud != null) {
			return Response.status(Status.BAD_REQUEST).build();
		} else {
			student.setId(getStudentDAO().addStudent(student));
			return Response.ok(student).build();
		}

	}

	@DELETE
	@Path("/student/{id}")
	@Consumes({"application/json","application/xml"})
	public Response deleteStudent(@PathParam("id") long id) {

		System.out.println("deleteStudent with student id : " + id);

		Student student = (Student) getStudentDAO().getStudent(id);
		if (student == null) {
			return Response.status(Status.BAD_REQUEST).build();
		} else {
			getStudentDAO().deleteStudent(id);
			return Response.ok().build();
		}
	}

	@PUT
	@Path("/student")
	@Consumes({"application/json","application/xml"})
	public Response updateStudent(Student student) {

		System.out.println("updateStudent with student id : "
				+ student.getId());

		Student stud = (Student) getStudentDAO().getStudent(
				student.getId());
		if (stud == null) {
			return Response.status(Status.BAD_REQUEST).build();
		} else {
			getStudentDAO().updateStudent(student);
			return Response.ok(student).build();
		}
	}

}

